package com.example.web.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 密码加密测试工具类 - MD5
 */
public class PasswordTest {
    
    public static String md5Encode(String rawPassword) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(rawPassword.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5算法不可用", e);
        }
    }
    
    public static void main(String[] args) {
        String rawPassword = "123456";
        
        // 生成MD5加密密码
        String md5Password = md5Encode(rawPassword);
        System.out.println("=== MD5密码加密测试 ===");
        System.out.println("原始密码: " + rawPassword);
        System.out.println("MD5加密后: " + md5Password);
        
        // 验证密码
        String testPassword = md5Encode("123456");
        boolean matches = md5Password.equals(testPassword);
        System.out.println("验证结果: " + matches);
        
        System.out.println("\n请使用以下SQL更新数据库:");
        System.out.println("UPDATE users SET password = '" + md5Password + "' WHERE username = 'admin';");
        System.out.println("UPDATE users SET password = '" + md5Password + "' WHERE username = 'test';");
        System.out.println("UPDATE users SET password = '" + md5Password + "' WHERE username = 'user1';");
    }
}